<template>
	<view class="home">
		<navbar :isSearch="true" @input="inputChange" v-model="value"></navbar>
		<view class="home-list">
			<view v-if="is_history" class="label-box">
				<view class="label-header">
					<text class="label-title">搜索历史</text>
					<text class="label-clear" @click="clear">清空</text>
				</view>
				<view class="label-content" v-if="historyList.length > 0">
					<view class="label-content-item" @click="itemSearch(item)" v-for="item in historyList">{{item.name}}
					</view>
				</view>
				<view v-else class="no-data">没有搜索历史</view>
			</view>
			<scroll-list v-else class="scroll-list">
				<uni-load-more v-if="loading" status="loading" iconType="snow"></uni-load-more>
				<view class="" v-if="searchList.length > 0">
					<card-list @open='setHistory' :item='item' v-for="(item,index) in searchList" :key="item._id">
					</card-list>
				</view>
				<view v-if="searchList.length===0 && !loading" class="no-data">
					没有搜索到数据
				</view>
			</scroll-list>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				is_history: true,
				searchList: [],
				value: '',
				loading: false
			}
		},
		computed: {
			...mapState(['historyList'])
		},
		// 监听页面卸载
		onUnload: function() {
			clearTimeout(this.timer)
		},
		methods: {
			inputChange(value) {
				if (!value) {
					clearTimeout(this.timer)
					this.mark = false
					this.searchArticle(value)
					return
				}
				// 限制请求的频率
				if (!this.mark) {
					this.mark = true
					this.timer = setTimeout(() => {
						this.mark = false
						this.searchArticle(value)
					}, 1000)
				}
			},
			searchArticle(value) {
				if (!value) {
					this.searchList = []
					this.is_history = true
					return
				}
				this.is_history = false
				this.loading = true
				this.$api.searchArticle({
					value
				}).then(res => {
					const {
						data
					} = res
					this.searchList = data
					this.loading = false
				}).catch((err)=>{
					this.loading = false
				})
			},
			setHistory() {
				this.$store.dispatch('set_history_list', {
					name: this.value
				})
			},
			itemSearch(item) {
				this.value = item.name
				this.searchArticle(this.value)
			},
			clear(){
				this.$store.dispatch('clear_history')
				uni.showToast({
					title:'清空完成'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		height: 100%;
		display: flex;
		background-color: #f5f5f5;
	}

	.home {
		display: flex;
		flex-direction: column;
		flex: 1;

		.label-box {
			background-color: #fff;
			border-bottom: 10px;

			.label-header {
				display: flex;
				justify-content: space-between;
				font-size: 14px;
				border-bottom: 1px solid #f5f5f5;
				color: #666;
				padding: 10px 15px;

				.label-title {
					color: $mk-base-color;
				}

				.label-clear {
					color: #30b33a;
					font-weight: bold;
				}
			}

			.label-content {
				display: flex;
				flex-wrap: wrap;
				padding: 15px;
				padding-top: 0;

				.label-content-item {
					padding: 2px 10px;
					margin-top: 12px;
					margin-right: 10px;
					border-radius: 5px;
					border: 1px solid #666;
					font-size: 14px;
					color: #666;
				}
			}

		}
		.no-data {
			height: 200px;
			line-height: 200rpx;
			width: 100%;
			text-align: center;
			color: #666;
			font-size: 12px;
		}
	}
</style>
